/*
 * handle_close.c
 *
 *  Created on: 2009-5-5
 *      Author: tian
 */

#include"tcpipsim.h"

void handle_close(msg *pmsg){
	sockmsg retmsg;
	memset(&retmsg,0,sizeof(sockmsg));
	struct tcpcb *ptcpcb=get_tcpcb_by_sock(pmsg->pid,pmsg->sock);
	if(ptcpcb==NULL){
		retmsg.mtype=pmsg->sock;
		retmsg.ret=-1;
		if(msgsnd(pmsg->msgqid,&retmsg,sizeof(sockmsg)-sizeof(long),0,0)<0){
					fprintf(stderr,"can not send message to application layer\n");
					return;
		}
	}
	else{
		retmsg.mtype=pmsg->sock;
		retmsg.ret=tcp_input(NULL,NULL,ptcpcb,close_sock);
		if(msgsnd(ptcpcb->msgqid,&retmsg,sizeof(sockmsg)-sizeof(long),0,0)<0){
			fprintf(stderr,"can not send message to application layer\n");
			return;
		}
	}
}
